﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using Model;

namespace BankingSystem.Dal.Repository
{
    public class BinaryRepository<T>:RepositoryBase<T>
        where T:IEntity
    {
        protected string _fileName;
        protected BinaryFormatter formatter = new BinaryFormatter();

        public override void Save()
        {
            using (var stream = File.Create(_fileName))
            {
                formatter.Serialize(stream, _local);
            }
        }
        public override void CheckOut()
        {
            using (var stream = File.OpenRead(_fileName))
            {
               _local=(Dictionary<string, T>) formatter.Deserialize(stream);
            }
        }

        public BinaryRepository(string fileName)
        {
            _fileName = fileName;
        }
    }
}
